/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.match.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.MainFrame;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.CharacterClickEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEventListener;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEventManager;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.SpriteSheetCache;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.key.AvatarFrameKey;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.key.PawnSheetKey;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.MatchManager;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.GenericPawn;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.exceptions.InvalidFileException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

/**
 * @author Massimo Canonaco
 *
 */
public class CharacterInformationContainerPanel016 extends JPanel implements GraphicEventListener
{
//	
///** */
//private ImageIcon noIcon;

/** */
private HashMap<String,ImageIcon> icons;

/** */
private SpriteSheetCache images;

 /** */
 private MatchManager matchManagerMonitor;
 
 /** */
 private CharacterAvatarInformationPanel016 characterAvatars;
 
// /** */
// private JLabel characterAvatar;
//
// /** */
// private JLabel characterName;
// 
// /** */
// private JProgressBar healthBar;
 
 /** */
 private Character actualCharacter;
 
 /** */
 private CharacterStatusPanel016 characterPanel;
 
 /** */
 private boolean ready;
 
 public CharacterInformationContainerPanel016(MainFrame mainFrame, MatchManager manager, SpriteSheetCache imgCache, Map<String, String> settings)
 {
	 super();
	 this.setLayout(new FlowLayout());
//	 setBackground(OverallGUILayoutFactory.getInstance().getParsedColor(settings.get("characterinformationpanel.background.color")));
	 characterAvatars = new CharacterAvatarInformationPanel016(imgCache,settings);
//	 characterAvatar = new JLabel();
	 icons = new HashMap<>();
	 try 
	 {
		 icons.put("noicon", new ImageIcon(ImageIO.read(new FileInputStream(settings.get("characterinformationpanel.noicon.path")))));
		 icons.put("bicon", new ImageIcon(ImageIO.read(new FileInputStream(settings.get("matchpanel.interactive.background.image.path")))));
//		noIcon = new ImageIcon(ImageIO.read(new FileInputStream(settings.get("characterinformationpanel.noicon.path"))));
	} catch (IOException e) 
	{
		e.printStackTrace();
	}
	 images = imgCache;
//	 setBackground(Color.red);
	 matchManagerMonitor = manager;
	 this.add(characterAvatars);
//	 this.add(characterAvatar);
//	 characterAvatar.setMaximumSize(new Dimension(150,150));
	 
	 characterPanel = new CharacterStatusPanel016(this,settings,imgCache);
	 this.add(characterPanel);
	 characterPanel.showBasePanel().disableComponents();
	 this.reset();
	 Dimension dimension = new Dimension(mainFrame.getWidth(),180);
	 this.setSize(dimension);
	 this.setPreferredSize(dimension);
	 this.setMinimumSize(dimension);
	 this.setMaximumSize(dimension);
//	 characterPanel.setMaximumSize(new Dimension(800,180));
	 
//	 GraphicEventManager.getInstance().addListener(this, CharacterClickEvent.class);
	 
 }
 
@Override
public void notify(GraphicEvent event) 
{
	if(event instanceof CharacterClickEvent)
	{
//		CharacterClickEvent a = (CharacterClickEvent) event;
//		actualCharacter = a.getClickedChar();
//		characterPanel.on();
	}
}


/**
 * @return the matchManagerMonitor
 */
public MatchManager getMatchManagerMonitor() {
	return matchManagerMonitor;
}


/**
 * @return the actualCharacter
 */
public Character getActualCharacter() {
	return actualCharacter;
}


/**
 * @param actualCharacter the actualCharacter to set
 */
public void setActualCharacter(Character actualCharacter, boolean on) 
{
	this.actualCharacter = actualCharacter;
	characterAvatars.setLabels(actualCharacter);
//	characterAvatar.setIcon(this.getIcon(actualCharacter.getType()));
//	System.out.println("IMPOSTO "+actualCharacter.getName());
	if(on)
		characterPanel.on();
	else
		characterPanel.off();
//	PawnSheetKey pawnKey = new PawnSheetKey();
//	pawnKey.setWho(actualCharacter.getType());
//	pawnKey.setWhat(PawnSheetKey.FRAME_TYPE_AVATAR);
//	pawnKey.setWhen("");
//	
//	AvatarFrameKey avatarKey = new AvatarFrameKey();
//	avatarKey.setKey(AvatarFrameKey.CLASSIC_AVATAR);
//	pawnKey.setFrameKey(avatarKey);
//	try {
//		characterAvatar.setIcon(new ImageIcon(images.getImage(pawnKey, 0, true)));
//	} catch (InvalidFileException e) 
//	{
//		// Image are inside the sheet, so this will never happen
//		e.printStackTrace();
//	}
}
 
 /** */
 public void reset()
 {
//	 System.out.println("RESETTATO PER QUALE MOTIVO NON SI SA FORZE");
	 this.actualCharacter = null;
	 characterPanel.getAttackPanel().reset();
	 characterPanel.showBasePanel().disableComponents();
//	 characterPanel.disableComponents();
//	 this.characterAvatar.setIcon(this.getIcon("noicon"));
	 characterAvatars.reset();
	 matchManagerMonitor.getClientPlayer().getActions().getCurrentAction().reset();
}

// /** */
// private Icon getIcon(String name)
// {
//	 if(icons.containsKey(name))
//		 return icons.get(name);
//
//		PawnSheetKey pawnKey = new PawnSheetKey();
//		pawnKey.setWho(actualCharacter.getType());
//		pawnKey.setWhat(PawnSheetKey.FRAME_TYPE_AVATAR);
//		pawnKey.setWhen("");
//		
//		AvatarFrameKey avatarKey = new AvatarFrameKey();
//		avatarKey.setKey(AvatarFrameKey.CLASSIC_AVATAR);
//		pawnKey.setFrameKey(avatarKey);
//		try 
//		{
//			icons.put(actualCharacter.getType(),new ImageIcon(images.getImage(pawnKey, 0, true)));
//		} catch (InvalidFileException e) 
//		{
//			// Image are inside the sheet, so this will never happen
//			e.printStackTrace();
//		}
//		
//		return icons.get(name);
// }

public boolean isReady() 
{
	return ready;
}

/**
 * @param ready the ready to set
 */
public void setReady(boolean ready) {
	this.ready = ready;
}

@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(icons.get("bicon").getImage(), 0, 0, getWidth(), getHeight(), null);
	}
}

